package com.deyang.mycommon.ui.activity.basics;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bm.library.PhotoView;
import com.deyang.mycommon.R;
import com.deyang.mycommon.config.ConfigRes;
import com.deyang.mycommon.config.IntentConfig;
import com.deyang.mycommon.ui.baseui.BaseActivity;
import com.deyang.mycommon.util.DensityUtil;
import com.deyang.mycommon.util.ImageUtil;
import com.deyang.mycommon.util.Logger;
import com.deyang.mycommon.util.MyRxBarTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 王德阳
 * @data 创建时间: 2017/12/1 0001
 * @Description 展示图片
 */
public class ShowImagesActivity extends BaseActivity {


    @BindView(R.id.vp_showImages)
    ViewPager vpShowImages;
    @BindView(R.id.rb_imageSelect)
    RadioGroup rbImageSelect;

    private List<String> imgs;
    private MyAdapter adapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        //状态栏透明化
        MyRxBarTool.transparentStatusBar(this);
        setContentView(R.layout.activity_show_images);
        ButterKnife.bind(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String imageUrls = intent.getStringExtra(IntentConfig.SHOW_IMAGES);
        int imagePsition = intent.getIntExtra(IntentConfig.SHOW_IMAGES_position, 0);

        imgs = new ArrayList<>();
        if (imageUrls != null) {
            String[] split = imageUrls.split(ConfigRes.ImageShowDivision);
            for (int i = 0; i < split.length; i++) {
                imgs.add(split[i]);
            }
        }

        if (imgs.size() > 1) {
            for (int i = 0; i < imgs.size(); i++) {
                Logger.e(TAG, imgs.get(i));

                RadioButton rb = new RadioButton(this);
                rb.setButtonDrawable(null);
                rb.setEnabled(false);
                rb.setBackgroundResource(R.drawable.rb_img_show);
                rbImageSelect.addView(rb);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(rb.getLayoutParams());
                params.width = DensityUtil.dip2px(this, 10);
                params.height = DensityUtil.dip2px(this, 10);
                int margin = DensityUtil.dip2px(this, 2);
                params.setMargins(margin, 0, margin, 0);
                rb.setLayoutParams(params);
            }
            rbImageSelect.check(rbImageSelect.getChildAt(imagePsition).getId());
        }


        adapter = new MyAdapter();
        vpShowImages.setAdapter(adapter);
        vpShowImages.setCurrentItem(imagePsition);
    }

    @Override
    public void setListener() {
        super.setListener();

        vpShowImages.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                rbImageSelect.check(rbImageSelect.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return imgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView view = new PhotoView(ShowImagesActivity.this);
            view.enable();
            view.setScaleType(ImageView.ScaleType.FIT_CENTER);

            ImageUtil.loadImage(mApp, view, imgs.get(position), R.drawable.load_fail);
            container.addView(view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
